package com.haogu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haogu.entity.Product;
import com.haogu.entity.ProductList;
import com.haogu.entity.User;
import com.haogu.service.ProductService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品控制器
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Resource
    private ProductService productService;
    /**
     * 跳产品列表主页面
     * @return
     */
    @RequestMapping("/toProduct")
    public String toProduct() {
        return "product/list_product";
    }

    /**
     * 展示所有产品信息
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/productList")
    @ResponseBody
    public Map productList(Integer page, Integer rows,String name,String suggestedprice) {
        //根据产品名称和零售价格进行模糊查询
        if (name != null && "".equals(name) || suggestedprice != null && "".equals(suggestedprice)) {
            List<ProductList> list = productService.selectProductsLike(page, rows, name, suggestedprice);
            Map map1 = new HashMap();
            int rows1 = productService.rows();//获取产品总数

            map1.put("totalRows", rows1);
            map1.put("result", list);
            Map map2 = new HashMap();
            map2.put("data", map1);
            return map2;
        }
        //查询所有
        Map map1 = new HashMap();
        int rows1 = productService.rows();//获取产品总数

        List<ProductList> list = productService.selectProducts(page, rows);
        map1.put("totalRows", rows1);
        map1.put("result", list);
        Map map2 = new HashMap();
        map2.put("data", map1);
        return map2;
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequestMapping("/toDel")
    @ResponseBody
    public Map toDel(String ids) {
        String[] split = ids.split(",");
        int del = 0;
        for (int i = 0; i < split.length; i++) {
            del = productService.del(Integer.valueOf(split[i]));
        }
        Map map = new HashMap();
        if (del > 0) {
            map.put("success", true);
            map.put("message", "删除成功");
        } else {
            map.put("success", false);
            map.put("message", "删除失败");
        }
        return map;
    }

    /**
     * 跳添加页面
     * @return
     */
    @RequestMapping("/toAddProduct")
    public String toAddProduct() {
        return "product/add_product";
    }

    /**
     * 添加产品
     *
     * @param product
     * @return
     */
    @RequestMapping("/toAdd")
    @ResponseBody
    public Map toAdd(Product product, HttpSession session) {
        User user = (User) session.getAttribute("user");
        product.setCreatorUserId(user.getId());//设置创建者
        int i = productService.addProduct(product);
        Map map = new HashMap();
        if (i == 1) {
            map.put("success", true);
            map.put("message", "添加成功");
        } else {
            map.put("success", false);
            map.put("message", "添加失败");
        }
        return map;
    }

    /**
     * 跳详情页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/toShow")
    public String toShow(Integer id, Model model) {
        Product product = productService.selectProduct(id);
        model.addAttribute("product", product);
        return "product/show_product";
    }

    /**
     * 跳修改页面
     *
     * @param id
     * @return
     */
    @RequestMapping("/toChange")
    public String toChange(@RequestParam("id") Integer id, Model model) {
        Product product = productService.selectProduct(id);
        model.addAttribute("product", product);
        return "product/change_product";
    }

    /**
     * 修改
     * @param product
     * @param modelMap
     * @return
     */
    @RequestMapping("/change")
    @ResponseBody
    public Map change(Product product, HttpSession session) {
        User user = (User) session.getAttribute("user");
        product.setCreatorUserId(user.getId());
        int change = productService.change(product);
        Map map = new HashMap();
        if (change == 1) {
            map.put("success", true);
            map.put("message", "修改成功");
        } else {
            map.put("success", false);
            map.put("message", "修改失败");
        }
        return map;
    }
}
